package practice.test2.name;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class RemovalMapper extends Mapper<LongWritable,Text, Text, NullWritable> {

    private Text outK = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //获得分割后的字符数组
        String[] split = value.toString().split(",");

        if(split.length == 2){
            //trim()方法功能除去字符串开头和末尾的空格或其他字符。
            String name_1 = split[0].trim();
            String name_2 = split[1].trim();

            if (!name_1.equals(name_2)){

                outK.set(
                        //通过名字字典大小，来统一顺序，方便reducer阶段去重
                        name_1.compareTo(name_2) > 0 ?
                                name_1+","+name_2:
                                name_2+","+name_1
                );
            }
        }
        context.write(outK,NullWritable.get());
    }
}
